<template>
  <div class="market">
    <div class="common-filter">
      <div class="header">
        <img src="/book.png" width="60" height="60" />
        <h1>收录图书列表</h1>
      </div>
      <div class="filter">
        <div class="filter-title">图书类别</div>
        <div class="filter-choices">
          <ul>
            <li
              v-for="item in bookCategories"
              :key="item.id"
              :class="item.active ? 'active' : ''"
              @click="handleCategoryClick(item)"
            >
              {{ item.name }}
            </li>
          </ul>
        </div>
      </div>
    </div>
    <div class="book-list" v-if="bookList.length > 0">
      <div v-for="item in bookList" :key="item.id">
        <LibraryBookCard :reload="reload" :book="item" />
      </div>
    </div>
    <div v-else>
      <el-empty :description="'暂无数据'" />
    </div>
    <div class="pg">
      <el-pagination
        :page-size="pageSize"
        layout="prev, pager, next"
        :total="totalCount"
        background
        :current-page="pageIndex"
        @current-change="handlePageChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { useApi } from "~/composeables";

const api = useApi();
const pageIndex = ref(1);
const pageSize = ref(9);
const totalCount = ref(0);
const bookList = ref<any>([]);
const categoryId = ref<any>([]);
const bookCategoriesResponse  = await api.bookCategoryApi.getAll();
const bookCategories = bookCategoriesResponse?.data;

const handleCategoryClick = (val: any) => {
  categoryId.value = [val.id];
  bookCategories.forEach((x : any) =>x.active=false);
  bookCategories.filter((x:any)=>x.id===val.id).forEach((x:any)=>x.active = true);
  filterData();
};

const filterData = () => {
  api.bookBookApi.filterData(
    { pageIndex: pageIndex.value, pageSize: pageSize.value, categoryId: categoryId.value })
    .then(res => {
      if (res.success) {
        bookList.value = res.data.pageData;
        totalCount.value = res.data.totalCount;
        pageIndex.value = res.data.pageIndex;
      }
    })
    .catch(err => {
      ElMessage.error("网络请求错误！");
    });
};

const handlePageChange = (page: any) => {
  pageIndex.value = page;
  filterData();
};

const reload = () => {
  filterData();
};


filterData();
</script>

<style scoped>

.market {
  width: 1200px;
  margin: 10px auto;
}

.common-filter {
  display: flex;
  flex-direction: column;
  padding-bottom: 25px;
  border-bottom: 2px solid #ccc;
}

.common-filter .header {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
}

.common-filter .header h1 {
  font-size: 2em;
  font-weight: 400;
  padding: 5px;
}

.common-filter .filter {
  display: flex;
  flex-direction: row;
  height: auto;
  margin: 10px 0;
}

.common-filter .filter .filter-title {
  font-size: 1.3em;
  font-weight: 600;
  height: auto;
  display: flex;
  justify-items: flex-start;
  align-items: center;
}

.common-filter .filter .filter-choices {
  flex: 1;
  margin: 0 0 0 20px;
  height: auto;
}

.common-filter .filter .filter-choices ul {
  margin: 0;
  padding: 0;
  display: block;
  list-style: none;
  background: white;
  border-radius: 5px;
  overflow: hidden;
}

.common-filter .filter .filter-choices ul li {
  margin: 5px 6px;
  width: auto;
  float: left;
  height: 30px;
  padding: 0px 10px;
  line-height: 30px;
  border-radius: 2px;
  cursor: pointer;
  color: #666;
}

.common-filter .filter .filter-choices ul li.active {
  background: #e2f1ff;
  color: #1a8ef4;
  font-weight: 600;
}

.book-list {
  display: flex;
  flex-wrap: wrap;
}

.pg {
  height: 65px;
  display: flex;
  width: 100%;
  justify-content: center;
}
</style>
